package com.shuyun.spring4.showcase.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

/**
 * Created by goodb on 2017/1/4.
 */
@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware {

    private String beanName;
    private ResourceLoader loader;

    @Override
    public void setBeanName(String name) {
        this.beanName = name;

    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;
    }

    public void outputResult(){
        System.out.println("bean的名称是：" + beanName);
        Resource resource = loader.getResource("classpath:test.txt");
        try{
            System.out.println("resource加载的文件内容是：" + IOUtils.toString(resource.getInputStream()));
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
